package cn.livingCloud.service.impl.operation;

import cn.livingCloud.context.BeanProvider;
import cn.livingCloud.context.SessionConstants;
import cn.livingCloud.dto.CaseCategoryDto;
import cn.livingCloud.dto.CaseListDto;
import cn.livingCloud.entity.core.Case;
import cn.livingCloud.entity.core.CaseCategory;
import cn.livingCloud.entity.core.Employee;
import cn.livingCloud.entity.user.Member;
import cn.livingCloud.repository.CaseCategoryMapper;
import cn.livingCloud.repository.CaseMapper;
import cn.livingCloud.repository.EmployeeMapper;
import cn.livingCloud.utils.SessionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by 唐旭 on 2016/6/13.
 */
public class CaseListDtoLoader {
    private transient CaseCategoryMapper categoryMapper = BeanProvider.getBean(CaseCategoryMapper.class);
    private transient EmployeeMapper employeeMapper = BeanProvider.getBean(EmployeeMapper.class);
    private transient CaseMapper caseMapper = BeanProvider.getBean(CaseMapper.class);
    private transient SessionDAO sessionDAO = BeanProvider.getBean(SessionDAO.class);

    private CaseListDto listDto;

    public CaseListDtoLoader(CaseListDto listDto) {
        this.listDto = listDto;
    }

    public void load() {
        loadCategory();
        privilegeCondition();
        setMod();
        List<Case> list = caseMapper.getList(listDto);
        listDto.addAllList(list);
    }

    private void privilegeCondition() {
        Member member = (Member) SessionUtils.getCurrentMember();
        if (member != null) {
            Employee employee = employeeMapper.findEmployeeByMember(member.getUuid());
            SessionUtils.setAttribute(SessionConstants.EMPLOYEE_KEY, employee);
        }

        Employee employee = SessionUtils.getAttribute(Employee.class, SessionConstants.EMPLOYEE_KEY);
        if (employee != null) {
            employee = employeeMapper.selectByPrimaryKey(employee.getUuid());
            SessionUtils.setAttribute(SessionConstants.EMPLOYEE_KEY, employee);
            listDto.setMulti(false);
        }
        //员工 1,如果是分派人员 则查找属于部门的案件
        if (employee != null && employee.isLeader()) {
            List<String> deptUuids = new ArrayList<>();
            ArrayList<String> lists = employee.getDept().getAllSubDeptUuid();
            deptUuids.addAll(lists);
            StringBuilder builder = new StringBuilder();
            for (String deptUuid : deptUuids) {
                builder.append(deptUuid).append(",");
            }
            if (builder.length() > 0) {
                builder.deleteCharAt(builder.length() - 1);
            }
            listDto.setDeptUuids(builder.toString());
        }
        //员工 2,如果不是分派人员 则查找属于自己的案件
        if (employee != null && !employee.isLeader()) {
            listDto.setOwnerUuid(employee.getUuid());
        }
        //如果是群众,则查找创建者为自己的案件
        Member currentMember = (Member) SessionUtils.getCurrentMember();
        if (employee == null && currentMember != null) {
            listDto.setCreatorUuid(currentMember.getUuid());
        }
    }

    public void setMod() {
        if (!listDto.isMulti()) {
            return;
        }
        Object currentUser = SessionUtils.getCurrentUser();
        if (currentUser == null) {
            return;
        }
        int index = -1;
        List<Session> userSession = new ArrayList<>();
        //如果是 客服 则按照规则查找
        Session currentSession = SecurityUtils.getSubject().getSession();
        Collection<Session> activeSessions = sessionDAO.getActiveSessions();
        for (Session session : activeSessions) {
            Object user = session.getAttribute(SessionConstants.USER_KEY);
            if (user != null) {
                userSession.add(session);
            }
            if (session.getId().equals(currentSession.getId())) {
                index = userSession.size();
            }
        }
        if (userSession.size() <= 1) {
            return;
        }
        listDto.setMax(userSession.size());
        listDto.setResultRow(index - 1);
    }

    private void loadCategory() {
        List<CaseCategory> all = categoryMapper.getAll();
        List<CaseCategoryDto> caseCategoryDtoList = listDto.getCaseCategoryDtoList();

        for (CaseCategory category : all) {
            if (category.getParent() == null) {
                caseCategoryDtoList.add(new CaseCategoryDto(category));
            }
        }
        addSubCategory(caseCategoryDtoList, all);
    }

    private void addSubCategory(List<CaseCategoryDto> categoryList, List<CaseCategory> all) {
        for (CaseCategoryDto caseCategoryDto : categoryList) {
            List<CaseCategoryDto> subList = caseCategoryDto.getSubList();
            for (CaseCategory category : all) {
                if (caseCategoryDto.getUuid().equals(category.getParentUuid())) {
                    subList.add(new CaseCategoryDto(category));
                }
            }
            addSubCategory(subList, all);
        }
    }
}
